// pages/demand_detail/demand_detail.js
var service_view = require("../../service/view.js");
var service_user = require("../../service/user.js");
var service_util = require("../../service/util.js");
var service_common = require("../../service/common.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    modal: '',
    replyList: [],
    page: 1,
    openid: null,
    options_id: null,
    val: "",
    userInfo: {},
    focus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.data != undefined && options.data != 0) {
      var optionss = JSON.parse(options.data)
      console.log(options)
      getApp().globalData.uniq_openid = optionss.uniq_openid
      wx.setStorageSync('token__', optionss.token)
      wx.setStorageSync('token_key', optionss.token_key)
      service_common.fetchData(
        service_common.getUrl("user.getUserMpInfo"), {
          openid: optionss.openid
        },
        (res) => {
          if (res.data.code == 0) {
            wx.setStorageSync('userInfo', res.data.data);
            getApp().globalData.userInfo = res.data.data
            getApp().globalData.openid = res.data.data.openid
            getApp().globalData.unionid = res.data.data.unionid
            this.setData({
              options_id: options.id,
              openid: getApp().globalData.userInfo.openid
            })
            service_view.getTopicDetail(options.id, this.TopicDetail);
            this.topicReplys()
          }
        },
        "GET"
      )
    }else{
      this.setData({
        options_id: options.id,
        openid: getApp().globalData.userInfo.openid
      })
      service_view.getTopicDetail(options.id, this.TopicDetail);
      this.topicReplys()
    }
  },
  TopicDetail: function(res) {
    this.setData({
      data: res.data.data
    })
    if (this.data.data.length == 0) {
      wx.showModal({
        title: '提示',
        content: '您访问的动态已不存在',
        success: function(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/page/index/index',
            })
          } else {
            wx.switchTab({
              url: '/page/index/index',
            })
          }
        }
      })
    }
    wx.stopPullDownRefresh();
  },
  topicReplys: function(e) {
    wx.showLoading({
      title: '数据加载中',
    })
    console.log(this.data.options_id)
    service_view.topicReplys(this.data.page, this.data.options_id, this.topicReplysPage)
    wx.stopPullDownRefresh();
  },
  love: function(e) {

    wx.navigateTo({
      url: '/pages/loveList/loveList?id=' + e.currentTarget.dataset.id,
    })

  },
  // play: function (e) {
  //   var ctx = wx.createVideoContext(e.currentTarget.id)
  //   ctx.requestFullScreen();//进入全屏
  //   console.log(e)
  // },
  end: function(e) {
    var ctx = wx.createVideoContext(e.currentTarget.id)
    ctx.exitFullScreen(); //退出全屏
  },
  topicReplysPage: function(res) {

    this.data.page++;
    var newReply = res.data.data.data;
    for (var i = 0; i < newReply.length; i++) {
      this.data.replyList.push(newReply[i]);
    }
    console.log(this.data.replyList)
    this.setData({
      reply: this.data.replyList,
      page: this.data.page
    })


    wx.hideLoading();
  },
  onPullDownRefresh: function() {
    this.topicReplys();
  },
  onReachBottom: function() {
    this.topicReplys();
  },
  goHome: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /*
   *靠谱点赞
   */
  setLike: function(e) {
    service_view.TopicLove(e.currentTarget.dataset.id, this.stateLike);
  },
  stateLike: function(res) {
    var that = this
    if (res.data.code == 0) {
      wx.showToast({
        title: '点赞成功',
      })
      service_view.getTopicDetail(this.data.options_id, this.TopicDetail);
      this.setData({
        showOperation: ""
      })
    } else {
      wx.showToast({
        title: '已点赞',
      })
      that.setData({
        showOperation: ""
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  format: function() {
    var reply = this.data.reply;
    for (var i = 0; i < reply.length; i++) {
      reply[i].time = service_util.getTime(reply[i].time);
    }
    this.setData({
      reply: reply,
      addTime: service_util.getTime(this.data.addTime)
    })
  },
  //图片预览
  showPhoto: function(e) {
    var id = e.currentTarget.id;
    id = parseInt(id);
    var array = [];
    for (var i = 0; i < this.data.data.thumb.length; i++) {
      array.push(this.data.data.thumb[i]);
    }
    wx.previewImage({
      current: array[id],
      urls: array
    })
  },
  formSubmit: function(e) {
    console.log(e)
    var content = e.detail.value.content
    var id = e.currentTarget.id;
    service_view.TopicReply(id, content, this.onFormSubmit);
    this.setData({
      val: ""
    })
  },
  onFormSubmit: function(res) {
    var that = this;
    if (res.data.code == 0) {
      that.setData({
        modal: '',
        showOperation: "",
        page: 1,
        replyList: [],
        reply: []
      })
      wx.showToast({
        title: '评论成功',
        icon: 'success',
        duration: 1000
      })
      service_view.topicReplys(this.data.page, this.data.options_id, this.topicReplysPage)
    } else {

      wx.showModal({
        title: '提示',
        content: res.data.message,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
          that.setData({
            showOperation: ""
          })
        }

      })
    }
  },
  //点击弹出操作栏 靠谱 评论 联系
  showOperation: function() {
    if (this.data.showOperation == 'show') {
      this.setData({
        showOperation: ''
      })
      return;
    }
    this.setData({
      showOperation: 'show'
    })
  },
  // 生成海报
  HB: function() {

    wx.previewImage({
      urls: ["http://check.libaizhaoshang.com/uri/user/getUserPsd?uid=" + this.data.data.id + "&&nickname=" + this.data.data.name + "&&oid=" + this.data.data.openid + "&&back=1&&version=" + getApp().globalData.version]
    })
  },
  //点击查看名片
  viewCard: function(e) {
    add_form_id(e.detail.formId);
    var openid = e.currentTarget.dataset.openid;

    wx.navigateTo({
      url: '/pages/other/other?openid=' + openid,
    })
  },
  //点击展开评论模态框
  modalShow: function() {
    this.setData({
      focus: true,
      modal: 'show',
      showOperation: ''
    })
  },
  modalHide: function(e) {
    var target_id = e.target.id;
    if (target_id == '') {
      this.setData({
        focus: false,
        modal: ''
      })
    }
  },
  onShareAppMessage: function(res) {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // 跳转名片夹
  goOther: function(e) {
    wx.navigateTo({
      url: '/pages/other/other?openid=' + e.currentTarget.dataset.openid,
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var that = this
    console.log(getApp().globalData.openid)
    return {
      title: getApp().globalData.userInfo.nickname + "'" + ",分享了一条动态",
      path: '/pages/auth/auth?webUrl=wrq&wrqid=' + this.data.options_id + '&zf_openid=' + getApp().globalData.userInfo.openid,
      success: function(res) {
        // 转发成功
        that.setData({
          showOperation: ""
        })
      },
      fail: function(res) {
        // 转发失败
        that.setData({
          showOperation: ""
        })
      }
    }
  }
})
//添加form_id
function add_form_id(form_id) {
  service_user.addFormId(form_id);
}